package com.chenchao.net;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/24 10:34
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test01.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * InetAddress IP测试
 */
public class Test01 {
    public static void main(String[] args) throws UnknownHostException {
        /*静态方法*/
        /*查询本机地址*/
        InetAddress byName = InetAddress.getByName("127.0.0.1");
        System.out.println(byName);
        /*/127.0.0.1*/
        System.out.println(InetAddress.getByName("localhost"));
        /*localhost/127.0.0.1*/
        System.out.println(InetAddress.getLocalHost());
        /*DESKTOP-TV2MUAQ/169.254.26.204*/

        /*查询网站ip*/
        System.out.println(InetAddress.getByName("www.baidu.com"));
        System.out.println("=============");

        /*注意，InetAddress的构造方法的访问控制符是缺省，不能直接new*/
        /*InetAddress对象常用方法*/
        InetAddress byName1 = InetAddress.getByName("九世轮回cc");

        /*获得ip字节数组*/
        byte[] address = byName1.getAddress();
        System.out.println(Arrays.toString(byName1.getAddress()));
        /*byte的范围为[-128,127],大于127的会被打印成负数*/
        /*[112, 80, -8, 76]*/

        /*获得规范名称getCanonicalHostName()*/
        System.out.println(byName1.getCanonicalHostName());
        /*112.80.248.76*/

        /*获得ip地址*/
        System.out.println(byName1.getHostAddress());
        /*112.80.248.76*/

        /*获得域名getHostName()*/
        System.out.println(byName1.getHostName());
        /*www.baidu.com*/

    }
}
